/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package sixdegrees.android;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.net.DatagramSocket;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.SocketException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author Fran
 */
public class VideochatServlet extends HttpServlet {
   static boolean primera=true;
    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    
    class HebraEscucha extends Thread{
        private boolean terminar=false;
        private int puerto=1238;
        Socket soc;
        InputStream flujo;
        ServerSocket serv;
        
        public HebraEscucha(){

            try {
                if(primera){
                    serv = new ServerSocket(puerto);

                    primera=false;
                }
               
            } catch (IOException ex) {
                Logger.getLogger(VideochatServlet.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        @Override
        public void run() {
            try {
             soc=serv.accept();
               BufferedInputStream bis= new BufferedInputStream(soc.getInputStream());
               FileOutputStream fos = new FileOutputStream(new File("C:\\Documents and Settings\\inftel\\Escritorio\\video.3gp"));
               BufferedOutputStream bos= new BufferedOutputStream(fos);
               int val;
               while((val=bis.read())!=-1)
                    bos.write(val);

               bos.close();
               bis.close();
               fos.close();
            } catch (IOException ex) {
                Logger.getLogger(VideochatServlet.class.getName()).log(Level.SEVERE, null, ex);
            }
         
        }

    };
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        
        HebraEscucha h=new HebraEscucha();
        h.start();
       //Mandar los puertos



    } 

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    } 

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
